package cn.btks.resultintegration.achievements.service.impl;

import cn.btks.commonserver.pojo.PageList;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.resultintegration.achievements.service.IServerService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

/**
 * @Author lijun
 * @Date 2020/12/21 16:58
 */
@Service
public class IServerServiceImpl implements IServerService {

    @Value("${iserver.layer}")
    private String url;

    @Value("${iserver.server}")
    private String urlServer;

    @Override
    public ResultState queryLayerData() {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//        headers.set("Authorization", GetToken.getToken());

        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);

        String responseBody = response.getBody();
        return !StringUtils.isEmpty(responseBody) ? new ResultState(StateInfo.FIND_OK.getCode(), StateInfo.FIND_OK.getMsg(),responseBody) :
                new ResultState(StateInfo.FIND_ERROR.getCode(), StateInfo.FIND_ERROR.getMsg());
    }

    @Override
    public ResultState queryServerData() {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//        headers.set("Authorization", GetToken.getToken());

        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange(urlServer, HttpMethod.GET, request, String.class);

        String responseBody = response.getBody();
        return !StringUtils.isEmpty(responseBody) ? new ResultState(StateInfo.FIND_OK.getCode(), StateInfo.FIND_OK.getMsg(),responseBody) :
                new ResultState(StateInfo.FIND_ERROR.getCode(), StateInfo.FIND_ERROR.getMsg());
    }
}
